@crpdo/coins
Cha Ching
![downloads](https://img.shields.io/npm/dw/@crpdo/coins)
![Discord](https://img.shields.io/badge/Basedwon%20-%20?logo=discord&color=%23383a40)
Coins is a utility class containing methods for working with different types of cryptocurrencies as part of the crpdo project. This module is particularly helpful for tasks like getting coin type IDs, deriving paths for a coin symbol, and getting Bitcoin or Ethereum addresses for given public keys, among other tasks.
Installation
npm install @crpdo/coins
Usage
Below are some examples of how to use the Coins module:
const Coins = require('@crpdo/coins')
const coinTypeId = Coins.getCoinType('BTC')
console.log(coinTypeId)
const derivePath = Coins.getDerivePath('ETH', 0, 0)
console.log(derivePath)
const btcAddress = Coins.getBtcAddress('somePublicKey', 'bitcoin')
console.log(btcAddress)
const isValidAddress = Coins.validateAddress('BTC', 'someAddress')
console.log(isValidAddress)
const coinInfo = Coins.getCoinInfo('ETH')
console.log(coinInfo)
const transactionFee = await Coins.getTransactionFee('BTC')
console.log(transactionFee)
const convertedAmount = await Coins.convertAmount('BTC', 1, 'ETH')
console.log(convertedAmount)
Please note that in order to use the getTransactionFee
and convertAmount
methods, you need to use the await
keyword because they are asynchronous.
Documentation
API
Coins.getCoinType(coinSymbol)
Get the coin type id for a given coin symbol.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- Number: The coin type id for the coin symbol.
Coins.getDerivePath(coinSymbol, account, change, index)
Get the derive path for a coin symbol, account, change, and optionally index.
Parameters:
coinSymbol
- String: The symbol for the coin. Default: 'BTC'.account
- Number: The account number. Default: 0.change
- Number: The change number. Default: 0.index
- Number: The index number. Optional.
Returns:
Coins.getNetwork(coinSymbol)
Set up network parameters for Dogecoin. This method currently only supports 'DOGE'.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'DOGE'.
Coins.getBtcAddress(pubkey, network)
Get a Bitcoin address for a given public key.
Parameters:
pubkey
- String|Buffer: The public key as a string or buffer.network
- String: The network name. Default: 'bitcoin'.
Returns:
- String: The Bitcoin address.
Coins.getEthAddress(pubkey)
Get an Ethereum address for a given public key.
Parameters:
pubkey
- String|Buffer: The public key as a string or buffer.
Returns:
- String: The Ethereum address.
Coins.getCoinName(coinSymbol)
Get the full name of a given coin.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- String: The full name of the coin.
- Throws an error if the coin symbol is not supported.
Coins.validateAddress(coinSymbol, address)
Validate a given address for a specific coin type.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.address
- String: The address to validate.
Returns:
- Boolean: True if the address is valid, false otherwise.
- Throws an error if the coin symbol is not supported.
Coins.getCoinInfo(coinSymbol)
Get a comprehensive set of information for a specific coin.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- Object: An object containing the coin information.
- Throws an error if the coin symbol is not supported.
Coins.getTransactionFee(coinSymbol)
Get the current average transaction fee for a given coin.
Parameters:
coinSymbol
- String: The symbol for the coin e.g. 'BTC'.
Returns:
- Number: The current average transaction fee.
- Throws an error if the coin symbol is not supported.
Coins.convertAmount(coinSymbol, amount, toCoinSymbol)
Convert an amount of one cryptocurrency to another.
Parameters:
coinSymbol
- String: The symbol for the source coin.amount
- Number: The amount of the source coin to convert.toCoinSymbol
- String: The symbol for the target coin.
Returns:
- Number: The equivalent amount in the target coin.
- Throws an error if either coin symbol is not supported.
Tests
In order to run the test suite, simply clone the repository and install its dependencies:
git clone https://gitlab.com/frenware/core/crpdo/coins.git
cd basd
npm install
To run the tests:
npm test
Contributing
Thank you! Please see our contributing guidelines for details.
Donations
If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you!
Bitcoin (BTC):
1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF
Monero (XMR):
46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ
License
@crpdo/coins is MIT licensed.